package com.kehutong.house.app;

import java.util.List;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.house.entity.Building;
import com.kehutong.house.entity.Community;
import com.kehutong.house.service.HouseService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import com.kehutong.house.entity.House;

/**
 * 房屋信息
 */
@MvcAction
@ReqMapping("/kht-bin/basic/house")
public class HouseAction {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private HouseService houseService;
    @Inject
    private DoveClient doveClient;

    /**
     * 小程序使用
     * @param jsonObject
     * @return
     */
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        List<House> list = jdbcSession.findArray(House.class)
                .eq(jsonObject,"id","communityNo", "buildNo")
                .eq("deleted", false)
                .order("buildName","ASC")
                .order("unitName","ASC")
                .order("name","ASC").exe();

        list.forEach(item->{
            String name = item.getOwnerName();
            if(Objects.nonEmpty(name)){
                if (name.length() > 2) {
                    item.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
                }else {
                    item.setOwnerName(name.substring(0, 1) + "*");
                }
            }else{
                item.setOwnerName("物业公司");
            }
        });

        return Result.success(list);
    }

    /**
     * 企微端使用
     * @param jsonObject
     * @return
     */
    @ReqMapping("/appList")
    public Object appList(JSONObject jsonObject) {
        return Result.success(houseService.permissionQuery(jsonObject));
    }

    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Token token, House house, JSONObject jsonObject) throws Exception {
        return houseService.insert(token, house, jsonObject);
    }

    @Bean(copy = true, isIgnoreNull = false)
    @ReqMapping("/update")
    public Object update(Token token, House house, JSONObject jsonObject) throws Exception {
        return houseService.update(token, house, jsonObject);
    }

    @ReqMapping("/delete")
    public Object delete(House house) throws Exception {
        JSONArray array = new JSONArray();
        JSONObject json = new JSONObject();
        json.put("id", house.getOwnerNo());
        json.put("type", "1");
        json.put("houseNo", house.getId());
        array.add(json);
        if (Objects.nonEmpty(array)) {
            doveClient.post("/crm/resident/service/batchDeleteResidentHouse", (http) -> {
                JSONObject body = new JSONObject();
                body.put("array", array);
                http.setBody(body.toJSONString());
            });
        }

        return houseService.delete(house);
    }

    @ReqMapping("/get")
    public Object get(JSONObject jsonObject) {
        House house = Pool.get(House.class,  jsonObject.getString("id"));

        String name = house.getOwnerName();
        if(Objects.nonEmpty(name)){
            if (name.length() > 2) {
                house.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
            }else {
                house.setOwnerName(name.substring(0, 1) + "*");
            }
        }else{
            house.setOwnerName("物业公司");
        }
        Community community = Pool.get(Community.class, house.getCommunityNo());

        Building build = Pool.get(Building.class, house.getBuildNo());
        JSONObject result = new JSONObject();
        result.put("house", house);
        result.put("community", community);
        result.put("build", build);

        return Result.success(result);
    }

    @ReqMapping("/appGet")
    public Object appGet(House house) {
        return Result.success(house);
    }

}
